#import "CustomActionSheet.h"
#import "AppDelegate.h"
#import "QGUIBasic.h"

#define intervalWithButtonsX 30
#define intervalWithButtonsY 5
#define buttonCountPerRow 4
#define headerHeight 60
#define bottomHeight 10
#define cancelButtonHeight 30
@implementation CustomActionSheet
@synthesize buttons;
@synthesize backgroundImageView;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithButtons:(NSArray *)buttonArray
{
    self = [super init];
    self.buttons = buttonArray;
    if (self) {
        // Initialization code
        coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        coverView.backgroundColor = [UIColor colorWithRed:51.0f/255 green:51.0f/255 blue:51.0f/255 alpha:0.6f];
        coverView.hidden = YES;
        
        UITapGestureRecognizer*tapRecognizerleft=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dis:)];
        [coverView addGestureRecognizer:tapRecognizerleft];
        
        UILabel* title = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SCREEN_SIZE_WIDTH, 35)];
        title.textColor = QGTextGrayL2;
        title.font = [UIFont systemFontOfSize:[Utils phoneFontL4]];
        title.textAlignment = NSTextAlignmentCenter;
        title.text = @"分享给朋友";
        
        UIView *sepline1 = [[UIView alloc]initWithFrame:CGRectMake(30,45,SCREEN_SIZE_WIDTH-60,0.5)];
        sepline1.backgroundColor = QGTextGrayL1;
        
        [self addSubview:title];
        [self addSubview:sepline1];
        
        for (int i = 0; i < [self.buttons count]; i++) {
            CustomActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.imgButton.tag = i;
            [button.imgButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview: button];
        }
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)dis:(UITapGestureRecognizer*)sender{
    [self dissmiss];
}

-(void)setPositionInView:(UIView *)view
{
    if([self.buttons count] == 0)
    {
        return;
    }
    
    float buttonWidth = ((CustomActionSheetButton *)[self.buttons objectAtIndex:0]).frame.size.width;
    
    float buttonHeight = ((CustomActionSheetButton *)[self.buttons objectAtIndex:0]).frame.size.height;
    
    self.frame = CGRectMake(0.0f, view.frame.size.height, view.frame.size.width, 30 + bottomHeight + headerHeight + (buttonHeight + intervalWithButtonsY)*(([self.buttons count]-1)/buttonCountPerRow + 1));
    
    self.backgroundImageView.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    
    int nButtons = buttonCountPerRow < self.buttons.count ? buttonCountPerRow : self.buttons.count;
    float beginX = (self.frame.size.width - (intervalWithButtonsX * (nButtons - 1) + buttonWidth * nButtons))/2;
    /*cancelButton.frame = CGRectMake(beginX,
                                    (intervalWithButtonsY + buttonHeight) * (([self.buttons count]-1)/buttonCountPerRow + 1) + headerHeight,
                                    self.frame.size.width - beginX * 2, cancelButtonHeight);*/
    if ([self.buttons count] > buttonCountPerRow) {
        for (int i = 0; i < [self.buttons count]; i++) {
            CustomActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.frame = CGRectMake(beginX + i%buttonCountPerRow*(buttonWidth + intervalWithButtonsX),
                                    headerHeight + i/buttonCountPerRow*(buttonHeight + intervalWithButtonsY), buttonWidth, buttonHeight);
        }
    }
    else
    {
        float intervalX = (self.frame.size.width - beginX*2 - buttonWidth*[self.buttons count])/([self.buttons count] - 1);
        for (int i = 0; i < [self.buttons count]; i++) {
            CustomActionSheetButton * button = [self.buttons objectAtIndex:i];
            button.frame = CGRectMake(beginX + i%buttonCountPerRow*(buttonWidth + intervalX),
                                      headerHeight + i/buttonCountPerRow*(buttonHeight + intervalWithButtonsY), buttonWidth, buttonHeight);
        }
    }
}

-(void)showInView:(UIView *)view
{
    [self setPositionInView:view];
    [view addSubview:coverView];
    [view addSubview:self];
    [UIView beginAnimations:@"ShowCustomActionSheet" context:nil];
    self.frame = CGRectMake(0.0f, self.frame.origin.y - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    coverView.hidden = NO;
    [UIView commitAnimations];
}

-(void)dissmiss
{
    [UIView beginAnimations:@"DismissCustomActionSheet" context:nil];
    self.frame = CGRectMake(0.0f, self.frame.origin.y + self.frame.size.height, self.frame.size.width, self.frame.size.height);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(sheetDidDismissed)];
    coverView.hidden = YES;
    [UIView commitAnimations];
}

-(void)sheetDidDismissed
{
    [coverView removeFromSuperview];
    [self removeFromSuperview];
}

-(void)buttonAction:(UIButton *)button
{
    NSLog(@"index:%ld",(long)button.tag);
    if([delegate respondsToSelector:@selector(choseAtIndex:)])
    {
        [delegate choseAtIndex:(int)button.tag];
    }
    [self dissmiss];
}
@end


@implementation CustomActionSheetButton
@synthesize imgButton;
@synthesize titleLabel;
-(id)init
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomActionSheetButton" owner:self options:nil] objectAtIndex:0];
    for (id obj in self.subviews) {
        if([obj isKindOfClass:[UIButton class]])
        {
            self.imgButton = obj;
        }
        else if([obj isKindOfClass:[UILabel class]])
        {
            self.titleLabel = obj;
        }
    }
    return self;
}

+(CustomActionSheetButton *)buttonWithImage:(UIImage *)image title:(NSString *)title
{
    CustomActionSheetButton * button = [[CustomActionSheetButton alloc] init];
    [button.imgButton setBackgroundImage:image forState:UIControlStateNormal];
    button.titleLabel.text = title;
    button.titleLabel.textColor = QGTextGrayL2;
    button.titleLabel.font = [UIFont systemFontOfSize:[Utils phoneFontL2]];
    return button;
}

@end